package com.beerandjesus.sportofkings.handicapper.factory;

import com.beerandjesus.sportofkings.handicapper.model.ProfileResult;
import com.beerandjesus.sportofkings.handicapper.model.ProfileResultImpl;
import com.beerandjesus.sportofkings.handicapper.model.brisnet.singlefile.Horse;
import com.beerandjesus.sportofkings.handicapper.model.brisnet.singlefile.Race;
import com.beerandjesus.sportofkings.handicapper.profile.Profile;
import org.apache.commons.lang.StringUtils;

import java.text.SimpleDateFormat;

/**
 * Date: 8/4/13
 * Time: 12:20 PM
 */
public class ProfileResultFactory {

    private static final String RACE_DATE_PATTERN = "yyyyMMdd";

    public static ProfileResult profileResult(Profile profile, Horse horse, Race race)
            throws BuildException {
        ProfileResultImpl result = new ProfileResultImpl();
        try {
            result.setAnalysisProfile(profile.getType().name());
            result.setScore(profile.getScore());
            result.setTrackCode(race.getTrack());
            result.setRaceNumber(Integer.valueOf(race.getRaceNumber()));
            result.setHorseName(horse.getName());
            if (!StringUtils.isEmpty(horse.getPostPosition())) {
                result.setPostPosition(Integer.valueOf(horse.getPostPosition()));
            }
            result.setRaceDate(new SimpleDateFormat(RACE_DATE_PATTERN).parse(race.getDate()));
        } catch (Exception e) {
            throw new BuildException("Exception building profile " + profile.getType() + " for horse " + horse.getName(), e);
        }
        return result;
    }

    public static class BuildException extends Exception {
        public BuildException(String s, Throwable throwable) {
            super(s, throwable);
        }
    }


}
